var defaultFontSize = Ti.Platform.name === 'android' ? 16 : 14;	

function ApplicationWindow(title) {
	
	var AddExpenseWindow = require('ui/handheld/AddExpenseWindow').AddExpenseWindow;
	var db = require('dbExpense');
	
	var self = Ti.UI.createWindow({
			title:title,
			backgroundColor:'white'
		});	
			
	self.addEventListener('focus', function(e) {
		self.currentTab = e.tab;
	});
	
	var tableData = getTableData();		
	var tableView = Ti.UI.createTableView({
		backgroundColor:'white',
		 data:tableData
	});
				
				
	self.add(tableView);
				
					
	self.addEventListener('open', function(){
		var activity = self.activity;
		activity.onCreateOptionsMenu = function(e) {
		var menu = e.menu;
		var menuItem = menu.add({ title: "Edit Expense" });
			menuItem.setIcon("images/ic_menu_add.png");
			menuItem.addEventListener("click", function(e) {
			new AddExpenseWindow().open();
	    });
		};
	});
	
	Ti.App.addEventListener('app:updateTables', function() {
		tableView.setData(getTableData());
	});
	
	tableView.addEventListener('click', function(e) {
		createConfirmDialog(e.row.id,e.row.title).show();
	});
				
/*	Ti.App.addEventListener('app:updateTables', function() {
		tableView.setData(getTableData());
	}); 
	
*/
					
	return self;
	
}; // End of Main Function

	var getTableData = function() {
		var db = require('dbExpense');
		var data = [];
		var row = null;
		var items = db.selectItems(); 
				
		for (var i = 0; i < items.length; i++) {
			row = populateRow(items[i]);
			data.push(row);
		}
		return data;
	}; /*End of getTableData*/

	var populateRow = function(_data){
			var row = Ti.UI.createTableViewRow({
	    		className:'forumEvent', // used to improve table performance
	    		selectedBackgroundColor:'white',
	    		id:_data.Id,  
	    		height:110
	  	});
	  		  
	 var labelItem = Ti.UI.createLabel({
		    color:'#576996',
		    font:{fontFamily:'Arial', fontSize:defaultFontSize+6, fontWeight:'bold'},
		    text:_data.Name,
		    left:5, top: 6,
		    width:200, height: 30
	  	});
	  		row.add(labelItem);
	  
	  var labelValue = Ti.UI.createLabel({
		    color:'#222',
		    font:{fontFamily:'Arial', fontSize:defaultFontSize+2, fontWeight:'normal'},
		    text:_data.Price,
		    left:5,top:44,
		    width:360
	  	});
	  		row.add(labelValue);
	  
	  
	  var labelDate = Ti.UI.createLabel({
		    color:'#999',
		    font:{fontFamily:'Arial', fontSize:defaultFontSize, fontWeight:'normal'},
		    text:'on ' + _data.Date,
		    left:5, bottom:10,
		    width:200, height:20
		});
	  		row.add(labelDate);
	  
	  return row;    
	  
	}; /*End of Populate row*/
	
var createConfirmDialog = function(id,title) {
		var db = require('dbExpense');	
		var UpdateExpenseWindow = require('ui/handheld/UpdateExpenseWindow').UpdateExpenseWindow; 	
		var buttons, clickHandler;
		buttons = ['Delete','Edit', 'Cancel'];	
		
		var clickHandler = function(e) {
			if (e.index === 0) {
				db.deleteItem(id);
				Ti.App.fireEvent('app:updateTables');
			}
			else if(e.index=== 1){
				//db.updateItem(id,name,price,date);
				new UpdateExpenseWindow(id).open();				
				Ti.App.fireEvent('app:updateTables');
			}					
		};

		var confirm = Ti.UI.createAlertDialog({
				title: 'Change Task Status',
				message: title,
				buttonNames: buttons
		});
		confirm.addEventListener('click', clickHandler);		
		return confirm;
};

module.exports = ApplicationWindow;
